﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace Networking
{
    public class PointerEventPacket : Packet
    {

        /// <summary>
        /// If mouse left button is down
        /// </summary>
        public bool LeftButton      { get; set; }

        /// <summary>
        /// If mouse middle button is down
        /// </summary>
        public bool MiddleButton    { get; set; }

        /// <summary>
        /// If mouse right button is down
        /// </summary>
        public bool RightButton     { get; set; }

        /// <summary>
        /// If mouse wheel is going up
        /// </summary>
        public bool WheelUp         { get; set; }

        /// <summary>
        /// If mouse wheel is going down
        /// </summary>
        public bool WheelDown       { get; set; }

        /// <summary>
        /// The mouse coordinates
        /// </summary>
        public Coords Coords        { get; private set; }

        /// <summary>
        /// Creates a new instance of PointerEventPacket with all the provided parameters
        /// </summary>
        /// <param name="LeftButton">If mouse left button is down</param>
        /// <param name="MiddleButton">If mouse middle button is down</param>
        /// <param name="RightButton">If mouse right button is down</param>
        /// <param name="WheelUp">If mouse wheel is going up</param>
        /// <param name="WheelDown">If mouse wheel is going down</param>
        /// <param name="Coords">The mouse coordinates</param>
        public PointerEventPacket(bool LeftButton, bool MiddleButton, bool RightButton, bool WheelUp, bool WheelDown, Coords Coords)
            : base(PacketType.PointerEvent)
        {
            this.LeftButton = LeftButton;
            this.RightButton = RightButton;
            this.WheelDown = WheelDown;
            this.WheelUp = WheelUp;
            this.MiddleButton = MiddleButton;
            this.Coords = Coords;
        }

        /// <summary>
        /// Creates a new instance of PointerEventPacket extracting all the parameters from the byte buffer
        /// </summary>
        /// <param name="buffer">The byte buffer containing the packet data</param>
        public PointerEventPacket(byte[] buffer)
            : base(PacketType.PointerEvent, buffer)
        {
            //
        }

        /// <summary>
        /// Serializes the packet in order to send it
        /// </summary>
        /// <returns>The bytes of the packet</returns>
        public override byte[] Serialize()
        {
            MemoryStream memory = new MemoryStream();

            BinaryWriter writer = new BinaryWriter(memory);

            BitArray mask = new BitArray(8);

            mask[0] = LeftButton;
            mask[1] = MiddleButton;
            mask[2] = RightButton;
            mask[3] = WheelUp;
            mask[4] = WheelDown;
            mask[5] = false;
            mask[6] = false;
            mask[7] = false;

            byte[] temp = new byte[1];
            mask.CopyTo(temp, 0);

            writer.Write((byte)this.Type);
            writer.Write(temp);
            writer.Write(this.Coords.X);
            writer.Write(this.Coords.Y);

            writer.Close();

            return memory.ToArray();            

        }


        /// <summary>
        /// Loads a packet from a byte buffer
        /// </summary>
        /// <param name="buffer">The data buffer</param>
        public override void Load(byte[] buffer)
        {
            if (buffer.Length != 6) throw new BufferSizeMismatchException(6);

            MemoryStream memory = new MemoryStream(buffer);

            BinaryReader reader = new BinaryReader(memory);

            this.Type = (PacketType)reader.ReadByte();

            if (this.Type != PacketType.PointerEvent)
                throw new PacketTypeMismatchException(PacketType.PointerEvent, this.Type);

            BitArray mask = new BitArray(reader.ReadBytes(1));

            LeftButton = mask[0];
            MiddleButton = mask[1];
            RightButton = mask[3];
            WheelUp = mask[3];
            WheelDown = mask[4];

            this.Coords = new Coords(reader.ReadInt16(), reader.ReadInt16());

            reader.Close();

        }
    }
}
